package com.opd.soeghtool;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UdpSocketHelper {
    private final String TAG ="UDP";
    private boolean bStopUdpRecv = false;
    private DatagramSocket socket_recv;
    private DatagramPacket packet;

    private int mTx_port,mRx_port;


    private byte g_u8Data[];
    private int g_s16Datalenth=0;

    public UdpSocketHelper(int tx_port,int rx_port) {
        g_u8Data = new byte[32];
        mTx_port = tx_port;
        mRx_port = rx_port;
    }

    private byte calcCheckSum(byte[] data, int len)
    {
        byte cks = 0;
        for(int a=0;a<len;a++)
        {
            cks+=data[a];
        }
        cks = (byte)(~cks);
        return cks;
    }
    //UDP数据发送
    public void sendCmd(byte[] data,int len){

        g_s16Datalenth = len+2;
        g_u8Data[0] = (byte)len;
        g_u8Data[1] = calcCheckSum(data,len);
        System.arraycopy(data,0,g_u8Data,2,len);
        Thread t = new UDPSendThread();
        t.start();
    }
    //发送数据线程
    public class UDPSendThread extends Thread{
        @Override
        public void run() {
            sendData_UDP();
        }
    }
    //UDP发送数据基层代码
    public void sendData_UDP(){
        try{
            //UDP协议Socket
            DatagramSocket socket_UDP = new DatagramSocket(1985);
            String ip = "127.0.0.1";
            InetAddress serverAddress = InetAddress.getByName(ip);
            //远程端口
            //int romotePort = 4000;
            DatagramPacket packet = new DatagramPacket(g_u8Data, g_s16Datalenth ,serverAddress , mTx_port);
            socket_UDP.send(packet); //发送数据包
            socket_UDP.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }





    public void receiveMessage(final OnRecvListener listener) {
        new Thread() {
            public void run() {
                try {
                    socket_recv = new DatagramSocket(mRx_port);
                } catch (SocketException e) {
                    e.printStackTrace();
                }
                byte[] receBuf = new byte[1024];
                packet = new DatagramPacket(receBuf, receBuf.length);
                while (!bStopUdpRecv) {
                    if(socket_recv==null)
                        break;
                    try {
                        socket_recv.receive(packet);
                        receBuf = packet.getData();
                        listener.onDataReceive(packet.getLength(),receBuf);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public interface OnRecvListener {
        void onDataReceive(int len,byte[] data);
    }

    public void SetStopUdpRecvThread(boolean val)
    {
        bStopUdpRecv = val;
    }

    public boolean getStopUdpRecvThread()
    {
       return bStopUdpRecv;
    }
}
